﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HYSoft.IDAL;
using HYSoft.DBUtility;//Please add references
namespace HYSoft.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:command
	/// </summary>
	public partial class command:Icommand
	{
		public command()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("index", "command"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int index)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from command");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HYSoft.Model.Zcommand model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into command(");
			strSql.Append("command,mac,flag,charvalue1,charvalue2,charvalue3,longvalue1,longvalue2,longvalue3)");
			strSql.Append(" values (");
			strSql.Append("@command,@mac,@flag,@charvalue1,@charvalue2,@charvalue3,@longvalue1,@longvalue2,@longvalue3)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@command", SqlDbType.Int,4),
					new SqlParameter("@mac", SqlDbType.Int,4),
					new SqlParameter("@flag", SqlDbType.Int,4),
					new SqlParameter("@charvalue1", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue2", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue3", SqlDbType.VarChar,255),
					new SqlParameter("@longvalue1", SqlDbType.Int,4),
					new SqlParameter("@longvalue2", SqlDbType.Int,4),
					new SqlParameter("@longvalue3", SqlDbType.Int,4)};
			parameters[0].Value = model.zcommand;
			parameters[1].Value = model.mac;
			parameters[2].Value = model.flag;
			parameters[3].Value = model.charvalue1;
			parameters[4].Value = model.charvalue2;
			parameters[5].Value = model.charvalue3;
			parameters[6].Value = model.longvalue1;
			parameters[7].Value = model.longvalue2;
			parameters[8].Value = model.longvalue3;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HYSoft.Model.Zcommand model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update command set ");
			strSql.Append("command=@command,");
			strSql.Append("mac=@mac,");
			strSql.Append("flag=@flag,");
			strSql.Append("charvalue1=@charvalue1,");
			strSql.Append("charvalue2=@charvalue2,");
			strSql.Append("charvalue3=@charvalue3,");
			strSql.Append("longvalue1=@longvalue1,");
			strSql.Append("longvalue2=@longvalue2,");
			strSql.Append("longvalue3=@longvalue3");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@command", SqlDbType.Int,4),
					new SqlParameter("@mac", SqlDbType.Int,4),
					new SqlParameter("@flag", SqlDbType.Int,4),
					new SqlParameter("@charvalue1", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue2", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue3", SqlDbType.VarChar,255),
					new SqlParameter("@longvalue1", SqlDbType.Int,4),
					new SqlParameter("@longvalue2", SqlDbType.Int,4),
					new SqlParameter("@longvalue3", SqlDbType.Int,4),
					new SqlParameter("@index", SqlDbType.Int,4)};
			parameters[0].Value = model.zcommand;
			parameters[1].Value = model.mac;
			parameters[2].Value = model.flag;
			parameters[3].Value = model.charvalue1;
			parameters[4].Value = model.charvalue2;
			parameters[5].Value = model.charvalue3;
			parameters[6].Value = model.longvalue1;
			parameters[7].Value = model.longvalue2;
			parameters[8].Value = model.longvalue3;
			parameters[9].Value = model.index;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int index)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from command ");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string indexlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from command ");
			strSql.Append(" where [index] in ("+indexlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.Zcommand GetModel(int index)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 [index],command,mac,flag,charvalue1,charvalue2,charvalue3,longvalue1,longvalue2,longvalue3 from command ");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			HYSoft.Model.Zcommand model=new HYSoft.Model.Zcommand();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.Zcommand DataRowToModel(DataRow row)
		{
			HYSoft.Model.Zcommand model=new HYSoft.Model.Zcommand();
			if (row != null)
			{
				if(row["index"]!=null && row["index"].ToString()!="")
				{
					model.index=int.Parse(row["index"].ToString());
				}
				if(row["command"]!=null && row["command"].ToString()!="")
				{
					model.zcommand=int.Parse(row["command"].ToString());
				}
				if(row["mac"]!=null && row["mac"].ToString()!="")
				{
					model.mac=int.Parse(row["mac"].ToString());
				}
				if(row["flag"]!=null && row["flag"].ToString()!="")
				{
					model.flag=int.Parse(row["flag"].ToString());
				}
				if(row["charvalue1"]!=null)
				{
					model.charvalue1=row["charvalue1"].ToString();
				}
				if(row["charvalue2"]!=null)
				{
					model.charvalue2=row["charvalue2"].ToString();
				}
				if(row["charvalue3"]!=null)
				{
					model.charvalue3=row["charvalue3"].ToString();
				}
				if(row["longvalue1"]!=null && row["longvalue1"].ToString()!="")
				{
					model.longvalue1=int.Parse(row["longvalue1"].ToString());
				}
				if(row["longvalue2"]!=null && row["longvalue2"].ToString()!="")
				{
					model.longvalue2=int.Parse(row["longvalue2"].ToString());
				}
				if(row["longvalue3"]!=null && row["longvalue3"].ToString()!="")
				{
					model.longvalue3=int.Parse(row["longvalue3"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select [index],command,mac,flag,charvalue1,charvalue2,charvalue3,longvalue1,longvalue2,longvalue3 ");
			strSql.Append(" FROM command ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" [index],command,mac,flag,charvalue1,charvalue2,charvalue3,longvalue1,longvalue2,longvalue3 ");
			strSql.Append(" FROM command ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM command ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.index desc");
			}
			strSql.Append(")AS Row, T.*  from command T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "command";
			parameters[1].Value = "index";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

